<template>
  <view class="pt-20 px-10">
    <view v-if="listData.length">
      <view v-for="(item, index) in listData" :key="index" @click="link(item)">
        <view class="grid-body content-box">
          <view class="info flex">
            <image src="/static/images/training-tasks/training.png" />
            <view class="ml-20 info-right">
              <view class="title">{{ item.trainName }}</view>
              <view class="my-10 font-12 color-BBBBBB"> 学时：{{ item.trainDurationLabel }} </view>
              <view class="my-10 font-12 color-BBBBBB"> 教师：{{ item.teacherNames }} </view>
              <view
                v-if="trainMethod === 1"
                :class="item.courseStatus === '3' ? 'courseStatus' : 'courseStatus timeout'"
              >
                {{ item.courseStatusLabel }}
              </view>
              <view
                :class="!item.hasEvaluate ? 'submit' : 'submit disabled'"
                @click.stop="handleEvaluation(item)"
                v-if="item.evaluateFlag === '1'"
              >
                评价
              </view>
            </view>
          </view>
          <view class="footer">
            <view class="my-10 font-12 color-BBBBBB">
              <text>培训时间：</text>
              <uni-dateformat :date="item.trainStartTime" format="yyyy/MM/dd hh:mm" />
              <text style="margin: 0 5px">-</text>
              <uni-dateformat :date="item.trainEndTime" format="yyyy/MM/dd hh:mm" />
            </view>
          </view>
        </view>
      </view>
    </view>
    <view v-else>
      <Empty />
    </view>
  </view>
</template>

<script>
import { get } from 'lodash'
import { getTrainingRecordList } from '@/api/educational-training/trainingTasks'
import dayjs from 'dayjs'
import Empty from '@/components/cu-empty/index.vue'

const foramtTime = (time) => dayjs(time).format('YYYY/MM/DD HH:mm')
export default {
  name: 'recordItem',

  components: {
    Empty
  },

  props: {},

  data() {
    return {
      pageNum: 1,
      trainMethod: 1,
      total: 0,
      listData: []
    }
  },

  computed: {},

  watch: {},

  created() {},

  mounted() {},

  destroyed() {},

  onShow() {
    this.initData()
  },
  onPullDownRefresh() {
    this.initData()
  },
  onReachBottom() {
    this.pageNum++
    this.getlist()
  },

  methods: {
    handleEvaluation(item) {
      if (!item.hasEvaluate) {
        uni.navigateTo({
          url: `/pages/work/training-tasks/teacherEvaluation?id=${item.trainPlanId}`
        })
      }
    },
    initData(tabIndex) {
      this.pageNum = 1
      this.total = null
      this.trainMethod = tabIndex + 1
      this.getlist()
    },
    getlist() {
      const formatRows = (rows) =>
        rows.map((item) => ({
          trainPlanId: item.taskId,
          trainName: item.planName,
          trainStartTime: foramtTime(item.startTime),
          trainEndTime: foramtTime(item.endTime),
          trainDuration: item.trainDuration,
          trainLearnedDuration: item.trainLearnedDuration,
          trainDurationLabel: item.trainDurationLabel,
          trainLearnedDurationLabel: item.trainLearnedDurationLabel,
          courseStatus: item.courseStatus,
          teacherNames: item.teacherNames,
          evaluateFlag: item.evaluateFlag,
          completionTime: item.completionTime,
          hasEvaluate: item.hasEvaluate,
          courseStatusLabel: item.courseStatusLabel
        }))
      if (!this.total || this.total > this.listData.length) {
        getTrainingRecordList({
          planMethod: this.trainMethod
        }).then((res) => {
          if (this.pageNum === 1) {
            this.listData = formatRows(res.rows)
          } else {
            this.listData.push(...formatRows(res.rows))
          }
          this.total = res.total
          uni.stopPullDownRefresh()
        })
      }
    },
    formatStatus(status, key) {
      if (!status) return ''
      const statusMap = {
        4: {
          className: 'bc-F04848',
          text: '已过期'
        },
        3: {
          className: 'bc-2AC0C8',
          text: '已完成'
        }
      }
      return get(statusMap, `${status}.${key}`)
    },
    link({ trainPlanId, trainName }) {
      if (this.trainMethod === 1) {
        uni.navigateTo({
          url: `/pages/work/training-tasks/content?trainPlanId=${trainPlanId}&pageName=${trainName}`
        })
      }
    }
  }
}
</script>

<style scoped lang="scss" type="text/scss">
.content-box {
  position: relative;
  margin-bottom: 40rpx;
  > image {
    width: 240rpx;
    height: 150rpx;
    border-radius: 10rpx 10rpx 10rpx 10rpx;
    flex-shrink: 0;
  }
  .progress {
    margin-top: 20rpx;
    width: 400rpx;
  }
}
.title {
  font-size: 32rpx;
  font-weight: bold;
  @include text-overflow-ellipsis(2);
}
.tag {
  position: absolute;
  top: 0;
  left: 0;
  width: 100rpx;
  height: 40rpx;
  border-radius: 10rpx 0rpx 34rpx 0rpx;
  color: #fff;
  line-height: 40rpx;
  text-align: center;
}
//.color-F04848
.bc-F04848 {
  background: #f04848;
}
.bc-2AC0C8 {
  background: #2ac0c8;
}
.content-box {
  margin-bottom: 30rpx;
  padding: 30rpx 30rpx 20rpx 30rpx;

  .info {
    position: relative;
    justify-content: space-between;
    align-items: center;
  }
  .info-right {
    flex: 1;
    height: 150rpx;
  }
  image {
    width: 150rpx;
    height: 150rpx;
  }
  .title {
    color: #131523;
    font-size: 32rpx;
    font-weight: bold;
    width: 350rpx;
    text-overflow: -o-ellipsis-lastline;
    overflow: hidden; //溢出内容隐藏
    text-overflow: ellipsis; //文本溢出部分用省略号表示
    display: -webkit-box; //特别显示模式		//特别显示模式
    -webkit-line-clamp: 1; //行数
    line-clamp: 2;
    -webkit-box-orient: vertical; //盒子中内容竖直排列
    word-break: break-all;
  }
  .time {
    font-size: 26rpx;
    color: #9b9fac;
    font-weight: 400;
    margin-top: 10rpx;
  }

  .courseStatus {
    position: absolute;
    right: 0;
    bottom: 65rpx;
    width: auto;
    line-height: 40rpx;
    height: 40rpx;
    font-size: 28rpx;
    background: #4ac425;
    padding: 0 24rpx;
    color: white;
    &.timeout {
      background: #cc9131;
    }
  }
  .submit {
    position: absolute;
    right: 0;
    bottom: 0;
    width: auto;
    line-height: 50rpx;
    height: 50rpx;
    font-size: 28rpx;
    border-radius: 32rpx 32rpx 32rpx 32rpx;
    background: #2ac0c8;
    padding: 0 24rpx;

    &.disabled {
      background: #ccc;
    }
  }
  .footer {
    margin-top: 20rpx;
    padding-top: 20rpx;
    border-top: 1px solid #f1f1f5;
  }
}
</style>
